(function($){
    $.fn.createPage = function(options){
        var defaults = {
            pageNum: 1,        //总页码
            current: 1,        //当前页
            backfun: function(e){}
        };
        var opts = $.extend({}, defaults, options);

        //创建页码
        create();

        function create(){
            var current = opts.current;    //当前页
            var pageNum = opts.pageNum;    //总页码
            var backfun = opts.backfun;    //回调函数

            var htmlStr = '';

            //上一页
            if(current == 1){
                htmlStr += '<a href="javascript:;" class="page disabled" data-page="prev">上一页</a>';
            }else{
                htmlStr += '<a href="javascript:;" class="page" data-page="'+(current-1)+'">上一页</a>';
            }

            //中间页码
            if(pageNum <= 5){
                for(var i=1; i<=pageNum; i++){
                    if(i == current){
                        htmlStr += '<a href="javascript:;" class="page current" data-page="'+i+'">'+i+'</a>';
                    }else{
                        htmlStr += '<a href="javascript:;" class="page" data-page="'+i+'">'+i+'</a>';
                    }
                }
            }else{
                if(current <= 3){
                    for(var i=1; i<=5; i++){
                        if(i == current){
                            htmlStr += '<a href="javascript:;" class="page current" data-page="'+i+'">'+i+'</a>';
                        }else{
                            htmlStr += '<a href="javascript:;" class="page" data-page="'+i+'">'+i+'</a>';
                        }
                    }
                    htmlStr += '<span>...</span>';
                    htmlStr += '<a href="javascript:;" class="page" data-page="'+pageNum+'">'+pageNum+'</a>';
                }else if(current >= pageNum-2){
                    htmlStr += '<a href="javascript:;" class="page" data-page="1">1</a>';
                    htmlStr += '<span>...</span>';
                    for(var i=pageNum-4; i<=pageNum; i++){
                        if(i == current){
                            htmlStr += '<a href="javascript:;" class="page current" data-page="'+i+'">'+i+'</a>';
                        }else{
                            htmlStr += '<a href="javascript:;" class="page" data-page="'+i+'">'+i+'</a>';
                        }
                    }
                }else{
                    htmlStr += '<a href="javascript:;" class="page" data-page="1">1</a>';
                    htmlStr += '<span>...</span>';
                    for(var i=current-2; i<=current+2; i++){
                        if(i == current){
                            htmlStr += '<a href="javascript:;" class="page current" data-page="'+i+'">'+i+'</a>';
                        }else{
                            htmlStr += '<a href="javascript:;" class="page" data-page="'+i+'">'+i+'</a>';
                        }
                    }
                    htmlStr += '<span>...</span>';
                    htmlStr += '<a href="javascript:;" class="page" data-page="'+pageNum+'">'+pageNum+'</a>';
                }
            }

            //下一页
            if(current == pageNum){
                htmlStr += '<a href="javascript:;" class="page disabled" data-page="next">下一页</a>';
            }else{
                htmlStr += '<a href="javascript:;" class="page" data-page="'+(current+1)+'">下一页</a>';
            }

            $(this).html(htmlStr);

            //绑定点击事件
            $(this).find('.page').click(function(){
                var page = $(this).data('page');
                if(page == 'prev'){
                    if(current > 1){
                        current--;
                    }
                }else if(page == 'next'){
                    if(current < pageNum){
                        current++;
                    }
                }else{
                    current = page;
                }

                //执行回调函数
                if(typeof backfun == 'function'){
                    backfun({
                        current: current
                    });
                }
            });
        }
    };
})(jQuery);